import Vue from 'vue'
import Router from 'vue-router'
import routes from './routers'
// import store from '@/store'
import { setToken, getToken } from '@/libs/util'

Vue.use(Router)

const router = new Router({
  routes,
  mode: 'hash'
})

const LOGIN_PAGE_NAME = 'login'
const REGISTER_PAGE_NAME = 'register'

router.beforeEach((to, from, next) => {
  const token = getToken()
  if (to.name === REGISTER_PAGE_NAME) {
    next()
  } else if (to.name === LOGIN_PAGE_NAME) {
    next()
  } else if (!token) {
    next({
      name: LOGIN_PAGE_NAME
    })
    // 正式环境将下一行去除掉！！！！
    setToken(1111)
  } else {
    next()
  }
})

router.afterEach(() => {
  window.scrollTo(0, 0)
})

export default router
